package org.learn.phase3.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceiveA {
    public static void main(String[] args) throws IOException {
        //1.创建一个 DatagramSocket  对象，准备在9999端口接收数据
        DatagramSocket socket = new DatagramSocket(9999);
        //创建一个 DatagramPacket    对象，准备接收数据
        //一个数据包最大64k
        byte[] buf = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        //调用方法，将通过网络传输的 DatagramPacket对象填充到packet对象
        socket.receive(packet);

        //把packet进行拆包，取出数据，并显示
        int length = packet.getLength();
        byte[] data = packet.getData();

        String s = new String(data, 0, length);
        System.out.println(s);

        //关闭流
        socket.close();
        System.out.println("a端退出");
    }
}
